<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/4 0004
 * Time: 下午 3:50
 */

namespace app\admin\controller;


use PhpMyAdmin\Server\Status\Data;
use think\Controller;
use think\Db;
use PhpOffice\PhpSpreadsheet\IOFactory;
define('DS', DIRECTORY_SEPARATOR);
defined('APP_PATH') or define('APP_PATH', dirname($_SERVER['SCRIPT_FILENAME']) . DS);
defined('ROOT_PATH') or define('ROOT_PATH', dirname(realpath(APP_PATH)) . DS);
/**
 * 项目资金管理控制器
 * Class Cat
 * @package app\admin\controller
 */

class Cat extends Authinfo
{
    //列表数据集
    public function index_cat(){
        $map=[];
        //模糊查询 项目名称
        $name=input('name');
        if($name){
            $map[]=array('name','like','%'.$name.'%');
        }
        $company_name=input('company_name');
        if($company_name){
            $map[]=array('department_id','like','%'.$company_name.'%');
        }
        // 分页
        $current=input('current');
        if(!$current){
            $current=1;
        }
        $pagenums=input('pagenums');
        if(!$pagenums){
            $pagenums=50;
        }
        $nowpagenums=($current-1)*$pagenums;
        //排序
        $order = input('order');
        if($order){
            $order='id DESC';
        }// 排序
        if($map){
            $total=Db::name('Cat')->where($map)->count('id');
            $info=Db::name('Cat')
                ->where($map)
                ->order($order)
                ->limit($nowpagenums,$pagenums)
                ->select();
        }else{
            $total=Db::name('Cat')->count('id');
            $info=Db::name('Cat')
                ->order($order)
                ->limit($nowpagenums,$pagenums)
                ->select();
        }

        if($info){
            foreach($info as $k=>$v){
                //通过部门id链表department 获取部门名称
                $data=Db::name('Department')->field('name,id')->where(['name'=>$v['department_id']])->find();
                $info[$k]['company_name']=$data['name'];
                $info[$k]['create_time']=time_nianyueri($v['create_time']);
                //审核状态
                if($v['status']==0){
                    $info[$k]['status']='未审核';
                }else{
                    $info[$k]['status']='审核通过';
                }
            }
            $datas['status']=1;
            $datas['info']=$info;
            $datas['total']=$total;
            $datas['current']=$current;
        }else{
            $datas['status']=0;
            $datas['info']='无数据！';
            $datas['total']=$total;
            $datas['current']=$current;
        }
        return json($datas);
    }
    //数据添加
    public function add_cat(){
        $name=input('name');
        if($name){
            $info['name']=$name;
        }else {
            exit;
        }
        $pid=input('pid');
        if($pid){
            $info['pid']=$pid;
        }
        $person=input('person');
        if($person){
            $info['person']=$person;
        }
        $department_id=input('department_id');
        if($department_id){
//            $info['department_id']=Db::name('Department')->where(['id'=>$department_id])->value('name');
        $info['department_id']=$department_id;
        }
        $cycle_time=input('cycle_time');
        if($cycle_time){
            $info['cycle_time']=$cycle_time;
        }
        $info['create_time']=time();
        $result=Db::name('Cat')
            ->where(['pid'=>$pid])
            ->where(['name'=>$name])
            ->where(['department_id'=>$department_id])
            ->find();
        if(!$result){
            if($info){
                $result=Db::name('Cat')->insert($info);
                if($result){
                    $datas['status']=1;
                    $datas['info']='新增成功！';
                }else{
                    $datas['status']=0;
                    $datas['info']='无数据！';
                }
            }else{
                $datas['status']=0;
                $datas['info']='无数据';
            }
        }else{
            $datas['status']=0;
            $datas['info']='数据已存在！';
        }

        return json($datas);
    }

    //新增表中的项目分类
    public function cat_type(){
        $info=Db::name('Cat_type')->select();
        if($info){
            $datas['status']=1;
            $datas['info']=$info;
        }else{
            $datas['status']=0;
            $datas['info']='无数据';
        }
        return json($datas);
    }

    //添加表单中单位分类
    public function list_company(){
        $info=Db::name('Department')->where('pid',0)->field('id,pid,name')->select();
        foreach($info as $k => $v){
            $info[$k]['son']=Db::name('Department')->where('pid',$v['id'])->field('id,pid,name')->select();
        }
        if($info){
            $datas['status']=1;
            $datas['info']=$info;
        }else{
            $datas['status']=0;
            $datas['info']='无数据！';
        }
        return json($datas);
    }
    //数据修改
    public function edit_cat(){
        $id=input('id');
        $info=[];
        $name=input('name');
        $pid=input('pid');
        $person=input('person');
        $department_id=input('department_id');
        $cycle_time=input('cycle_time');  $status=input('status');
        if($id){
            if($name!=''){
                $info['name']=$name;
            }
            if($pid!=''){
                $info['pid']=$pid;
            }

            if($person){
                $info['person']=$person;
            }

            if($department_id){
                $info['department_id']=$department_id;
            }

            if($cycle_time){
                $info['cycle_time']=$cycle_time;
            }

            if($status){
                $info['status']=$status;
            }
            if($info){
                $result=Db::name('Cat')->where(['id'=>$id])->update($info);
                if($result){
                    $datas['status']=1;
                    $datas['info']='修改成功！';
                }else{
                    $datas['status']=0;
                    $datas['info']='无数据！';
                }
            }else{
                $datas['status']=0;
                $datas['info']='无数据';
            }
        }else{
            $datas['status']=0;
            $datas['info']='缺少参数！';
        }
        return json($datas);
    }
    //修改回显
    public function edit_view(){
        $id=input('id');
        $list=Db::name('Cat')->where(['id'=>$id])->find();
        $list['create_time']=date('Y-m-d H:i:s',$list['create_time']);

        if($list){
            $datas['status']=1;
            $datas['info']=$list;
        }else{
            $datas['status']=0;
            $datas['info']='无数据!';
        }
        return json($datas);
    }
    //批量审核
    public function examine_all(){
        $idArray=input('id');
            if(is_array($idArray)){
                $info['status']=1;
                $result = Db::name('Cat')
                    ->where('id', 'in', $idArray)
                    ->update($info);
                if($result){
                    $datas['status'] = 1;
                    $datas['info'] = '成功';
                }else{
                    $datas['status'] = 0;
                    $datas['info'] = '失败！';
                }
            }else{
                $datas['status']=0;
                $datas['info']='无参数！';
            }


        return json($datas);
    }

    /**批量取消审核
     * @return \think\response\Json
     */
    public function examine_cancel(){
        $idArray=input('id');
            if(is_array($idArray)){
                $info['status']=0;
                $result = Db::name('Cat')
                    ->where('id', 'in', $idArray)
                    ->update($info);
                if($result){
                    $datas['status'] = 1;
                    $datas['info'] = '成功';
                }else{
                    $datas['status'] = 0;
                    $datas['info'] = '失败！';
                }
            }else{
                $datas['status']=0;
                $datas['info']='无参数！';
            }


        return json($datas);
    }
    //数据删除
    public function delete_cat(){
        $ids=input('id');
            if(is_array($ids)){
                $info=Db::name('Cat')->where('id','in',$ids)->delete();
                if($info){
                    $datas['status']=1;
                    $datas['info']=$info;
                }else{
                    $datas['status']=0;
                    $datas['info']='无数据！';
                }
            }else{
                $datas['status']=0;
                $datas['info']='无参数！';
            }
        return json($datas);
    }

    public function pladd(){
        $pathurl=input('pathurl');
//        $pathurl='uploads\wenjian\20180619\20180804.xlsx';
//        截取获得文档标题
//        $data=substr($pathurl,-13);
        $startRow=input('startRow');

        if(!$startRow){
            $startRow=2;
        }
        if($pathurl){
            $lujing=$pathurl;
            $excelFile=ROOT_PATH.$lujing;
//            $excelReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader("Xlsx");
            $excelReader =IOFactory::createReader('Xlsx');
            $excelReader->setReadDataOnly(true);
            $endRow = input('endRow');
            if (!$endRow) {
                $endRow = 2000;
            }

            $filterSubset=new Ireadefilter($startRow, $endRow,range('A', 'H'));
            $excelReader->setReadFilter($filterSubset);
            $phpexcel    = $excelReader->load($excelFile);
//			$activeSheet = $phpexcel->setActiveSheetIndex(9);

            $activeSheet = $phpexcel->getActiveSheet();

            $zongshu=$activeSheet->getHighestRow(); //总行数
            if($endRow>$zongshu){
                $endRow=$zongshu;
            }
            if($startRow<=$zongshu){

                $sheetData = $activeSheet->toArray(null, true, true, true);

                $highestColumn      = $activeSheet->getHighestColumn(); //最后列数所对应的字母，例如第2行就是B

                $data = array();

                for ($col = $startRow; $col <=$endRow; $col++) {
                    if($sheetData[$col]){
                        $data[] = $sheetData[$col];
                    }


                }
                $phpexcel->disconnectWorksheets();
                unset($phpexcel);

                $shujudata=array();


                if($data){
                    $nums=0;
                        $shujudata['name']=$value['A'];
                        $shujudata['pid']=$value['B'];
                        $shujudata['person']=$value['C'];
                        $shujudata['amount']=$value['D'];
                        $shujudata['content']=$value['E'];
                        $shujudata['status']=$value['F'];
                        $shujudata['cycle_time']=$value['G'];
                        $shujudata['department_id']=$value['H'];
                        if($value['A']){//干部sfid存在
                            //查询是否存在数据库中
                            $cz=Db::name('Cat')
                                ->where('name',$value['A'])
                                ->find();
                            if(!$cz){
                                //不存在则进行添加操作
                                $shujudata['create_time']=time();
                                $idss=Db::name('Cat')->insert($shujudata);
                                if($idss){
                                    $nums++;
                                    unset($shujudata);
                                }
                            }else{//存在则进行重复数据统一操作
                                $shujudata['create_time']=time();
                                $gx=Db::name('Cat')->where('id',$cz['id'])->update($shujudata);
                                $nums++;
                                unset($shujudata);
                            }
                        }
                    $datas['status']=1;
                    $tiaoshu=$endRow;
                    $datas['cginfo']=$nums;
                    $datas['nextnums']=$endRow+1;
                    $datas['isend']=0;
                    unset($data);

                }else{
                    $datas['status']=0;
                    $datas['info']='无数据！';

                }


            }else{
                $datas['status']=1;
                $datas['info']='导入完毕';
                $datas['isend']=1;

            }


        }else{
            $datas['status']=0;
            $datas['info']='文件不存在';


        }
        return  json($datas);

    }
}